package com.lushu.chapitre1.graphics.controls
{
    import com.lushu.chapitre1.events.NPCEvent;
    import com.sfdk.core.KeyboardListener;
    import com.sfdk.graphics.core.TextFieldEx;
    import com.sfdk.graphics.ui.parts.ComponentBase;
    import com.sfdk.pools.BitmapDataPool;
    import com.sfdk.pools.MusicPool;

    import flash.display.Bitmap;
    import flash.display.Graphics;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.media.SoundChannel;

    import spark.primitives.Graphic;

    public class TextBox extends ComponentBase
    {
        private var _textBoxBackground:Bitmap;
        private var _textBoxTextField:TextFieldEx;

        private var _moreText:Bitmap;

        private var _currentFrame:int;
        private var _textDisplayed:Boolean;
        private var _currentText:String
        private var _charIndex:int;
        private var _displaySpeed:int = 1;
        private var _currentDisplayFrame:int = 0;
        private var _currentChannel:SoundChannel;
        private var _noSoundPlaying:Boolean = true;
        private var _hasMore:Boolean;
        private var _mask : Sprite;
        private var _lineBreakCount : int = 0;

        public function TextBox(textToDisplay:String = "") {
            _currentText = textToDisplay;
            super();
        }

        override protected function createChildren():void {
            super.createChildren();

            _moreText = BitmapDataPool.instance.getAsBitmap("resources/images/moreText.png");
            _moreText.visible = false;

            _textBoxBackground = BitmapDataPool.instance.getAsBitmap("resources/images/textBox.png");
            _mask = new Sprite();
            _textBoxTextField = new TextFieldEx("SF-12");
            _textBoxTextField.width = _textBoxBackground.width;
            _textBoxTextField.height = _textBoxBackground.height - 20;
        }

        override protected function mapHotKeys():void {
            super.mapHotKeys();
        }

        override protected function cleanView(e:Event):void {
            super.cleanView(e);
            KeyboardListener.instance.removeEventListener("ENTER", onNextPage);
        }

        override protected function createView(e:Event):void {
            super.createView(e);

            attachChild(_textBoxBackground, 0, 0, "textBoxBackground");
            attachChild(_textBoxTextField, 18, 16, "textBoxTextField");

            attachChild(_moreText, 432, 96, "moreText");

            var g : Graphics = _mask.graphics;
            g.beginFill(0xFFFFFF);
            g.drawRect(0, 0, _textBoxBackground.width, _textBoxBackground.height - 20);
            g.endFill();
            //attachChild(_mask);
            //_textBoxTextField.mask = _mask;

            if(_currentText)
                _currentText = _currentText.replace("${user}", "AAAAAAA").replace(/\\n/g, "\n");
            _charIndex = 0;
            KeyboardListener.instance.addEventListener("ENTER", onNextPage);
            KeyboardListener.instance.addEventListener("Keyboard-13-UP", onEnterUp);
        }

        override public function update():void {
            super.update();
            if (_textDisplayed && _currentFrame <= 0 && _hasMore) {
                _moreText.visible = !_moreText.visible;
                _currentFrame = 8;
            }
            else {
                _currentFrame--;
            }
            if (_currentText && _currentDisplayFrame >= _displaySpeed) {
                if (_charIndex < _currentText.length) {
                    _moreText.visible = false;
                    var nextChar : String = _currentText.charAt(_charIndex);
                    if(nextChar != "" && nextChar != " ")
                    {
                        _currentChannel = MusicPool.getFromPool("resources/sounds/Voice 1.mp3").play(50);
                    }
                    if(nextChar == "\n")
                    {
                        _lineBreakCount++;
                    }
                    _textBoxTextField.scrollV = _lineBreakCount + 1;
                    _textBoxTextField.text += nextChar;
                    _charIndex++;
                }
                else {
                    _textDisplayed = true;
                }
                _currentDisplayFrame = 0;
            }
            else {
                _currentDisplayFrame++;
            }

        }

        /**
         *  UI Handlers
         */

        private function onNextPage(e:Event):void {
            if (!_textDisplayed)
            {
                _displaySpeed = 0;
            }
            else {
                if (_displaySpeed == 1 && _hasMore) {
                    this.dispatchEvent(new NPCEvent(NPCEvent.NEXT_PAGE));
                }
                else if(_displaySpeed == 1 && !_hasMore)
                {
                    this.dispatchEvent(new NPCEvent(NPCEvent.END));
                }
            }
        }

        public function setContentText(textToDisplay : String, hasMore : Boolean) : void
        {
            _currentText = textToDisplay.replace("${user}", "AAAAAAA").replace(/\\n/g, "\n");
            _hasMore = hasMore;
            _textDisplayed = false;
            _charIndex = 0;
        }

        private function onEnterUp(e:Event) : void
        {
            _displaySpeed = 1;
        }
    }
}
